package org.emample

import scala.collection.mutable.ArrayBuffer
object zy {
  def acc(array1:ArrayBuffer[Int],array2:ArrayBuffer[Int],array3:ArrayBuffer[Int]){
    for(i <- 2 to courseNames.length+1){
      for (j<- 0 to allStudents.length-1){
        array1.append(allStudents(j)(i).toInt)
      }
    }
    for(i <- 2 to courseNames.length+1){
      for (j<- 0 to allStudents.length-1){
        if(allStudents(j)(1)=="male"){
          array2.append(allStudents(j)(i).toInt)
        }
      }
    }
    for(i <- 2 to courseNames.length+1){
      for (j<- 0 to allStudents.length-1){
        if(allStudents(j)(1)=="female"){
          array3.append(allStudents(j)(i).toInt)
        }
      }
    }
  }

  def get_data(array:ArrayBuffer[Int],data:ArrayBuffer[Double],data2:ArrayBuffer[Double],Nn:Int){
    while (!array.isEmpty){
      for (i <-0 to Nn.toInt-1){
        data.append(array(i))
      }
      array.trimStart(Nn)
      data2.append((data.sum/data.length).formatted("%.2f").toDouble)
      data2.append(data.min.formatted("%.2f").toDouble)
      data2.append(data.max.formatted("%.2f").toDouble)
      data.trimEnd(Nn)
    }
  }

  def print_data(data:ArrayBuffer[Double]){
    for (i<-0 to courseNames.length-1){
      print(courseNames(i)+":  ")
      for(j<-0 to 2){
        print(data(j)+"  ")
      }
      println()
      data.trimStart(3)
    }
  }
  val File = scala.io.Source.fromFile("word/test2.txt")
  import scala.io.Source

  object ReadFileExample {
    def main(args: Array[String]): Unit = {
      val sourceFromFile = Source.fromFile("word/test1.txt")

      sourceFromFile.getLines().foreach(println)

      sourceFromFile.close()
    }
  }
  val all_Data = File.getLines().map{_.split(" ")}.toList
  val courseNames = all_Data.head.drop(2)
  courseNames(0)="Math   "
  val allStudents = all_Data.tail

  def main(args: Array[String]): Unit = {

    val array1=ArrayBuffer[Int]()
    val array2=ArrayBuffer[Int]()
    val array3=ArrayBuffer[Int]()

    acc(array1,array2, array3)


    val data=ArrayBuffer[Double]()
    val data2=ArrayBuffer[Double]()
    val Nn=(array1.length/courseNames.length)

    val male_data2=ArrayBuffer[Double]()
    val male_Nn=array2.length/courseNames.length

    val  female_data2=ArrayBuffer[Double]()
    val  female_Nn=array3.length/courseNames.length


    get_data(array1,data,data2, Nn)
    get_data(array2,data,male_data2,male_Nn)
    get_data(array3,data,female_data2,female_Nn)


    println("course  "+"average  "+"min  "+"max  ")
    print_data(data2)
    println("course  "+"average  "+"min  "+"max(male)  ")
    print_data(male_data2)
    println("course  "+"average  "+"min  "+"max(female)  ")
    print_data(female_data2)


  }


}